package com.EM1.www.servlet;

import com.EM1.www.dao.ConnectQuestion;
import com.EM1.www.po.Paper;
import com.EM1.www.po.Question;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/PaperDetail")
public class PaperDetail extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        int index = Integer.parseInt(request.getParameter("index"));
        HttpSession session = request.getSession();
        ArrayList<Paper> papers =(ArrayList<Paper>) session.getAttribute("papers");
        Paper paper = papers.get(index);
        ConnectQuestion connectQuestion=new ConnectQuestion();
        try {
            ArrayList<Question> questions =connectQuestion.selectQuestion(paper.getQuestionId());
            request.setAttribute("questions",questions);
            session.setAttribute("paper",paper);
            String filer = request.getParameter("filer");
            System.out.println(filer);
            if(filer.equals("1")){
                request.getRequestDispatcher("paperdetail.jsp").forward(request,response);
            }else {
                request.getRequestDispatcher("changepaper.jsp").forward(request,response);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
